#!/bin/bash
#
# Shell API implementation of the SSH module
. /var/opencore/api/sh/module.sh

SSH:Shell.create() {
	user=`coreval User id`
	id "$user" >/dev/null 2>&1 || exiterror "User does not exist"
	newshell=`coreval SSH:Shell shell`
	if [ "$newshell" == "noshell" ]; then
	  authd setusershell "$user" "/bin/false"
	else
	  grep -q "/$newshell\$" /etc/shells || exiterror "Shell $newshell not in /etc/shells"
	  realshell=`sed 's/#.*//' /etc/shells | grep "/$newshell\$" | head -1`
	  authd setusershell "$user" "$realshell"
	fi
	sshkey=`coreval SSH:Shell pubkey | grep "^[ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/=+-@.]*$"`
	if [ ! -z "$sshkey" ]; then
	  try_authd makeuserdir "$user" 0750 .ssh
	  echo "$sshkey" > authorized_keys
	  chmod 600 authorized_keys
	  authd installuserfile authorized_keys ".ssh" "$user"
	fi
}

SSH:Shell.update() {
	SSH:Shell.create
}

SSH:Shell.delete() {
	user=`coreval User id`
	authd setusershell "$user" /bin/false
}

implement SSH.module
